package com.isoftstone.merchant.controller.mprojecttype;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.mprojecttype.MProjectType;
import com.isoftstone.merchant.service.mprojecttype.MProjectTypeService;
import com.isoftstone.util.Const;
import com.isoftstone.util.PageData;
import com.isoftstone.util.StringUtils;
import com.isoftstone.util.UuidUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by as on 2017/12/12.
 * 项目类型设置
 */
@Controller
public class MprojectTypeController extends BaseController{
    @Autowired
    private MProjectTypeService mProjectTypeService;
    /*
    * 到达项目类型设置页面
    * */
    @RequiresPermissions("mms-menu.003.001")
    @RequestMapping("merchant/mProjectType/getProjectTypeList")
    public ModelAndView toMProjectType(){
        ModelAndView md=new ModelAndView();
        md.setViewName("merchant/charge_management/project_type_set");
        return md;
    }
    @RequestMapping("merchant/mProjectType/getProjectTypeList1")
    @ResponseBody
    public Object mProjectTypeList(Page page){
        PageData pd = this.getPageData();
        Tidings<List<MProjectType>> tidings = new Tidings<List<MProjectType>>();
        String keywords=pd.getString("searchContent");
        String corpId=this.getUser().getCorpId();
        pd.put("corpId",corpId);
        try {
            if(StringUtils.isNotBlank(keywords)){
                keywords = URLDecoder.decode(keywords, "UTF-8");
                pd.put("keywords",keywords);
            }
            page.setPd(pd);
            List<MProjectType> mProjectTypeList = mProjectTypeService.findprojecttypelistPage(page);
            tidings.setT(mProjectTypeList).setPage(page);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings ;
    }
    @RequestMapping("merchant/mProjectType/saveOrUpdateMProjectType")
    @ResponseBody
    public Object addMProjectType(MProjectType mProjectType){
        PageData pd = this.getPageData();
        Tidings<List<MProjectType>> tidings = new Tidings<List<MProjectType>>();
        String corpId=this.getUser().getCorpId();
        String userId=this.getUser().getUserid();
        Map<String,Object>map=new HashMap<String,Object>();
        map.put("cropId",corpId);
        List<MProjectType>  MP=null;
        try {
            if(StringUtils.isNotBlank(mProjectType.getProjectTypeName())){
                map.put("name",mProjectType.getProjectTypeName());
                if(mProjectType.getProjectTypeId()!=null){
                    MP=mProjectTypeService.checkName(map);
                    if(MP.size()==1){
                      if(MP.get(0).getProjectTypeId().equals(mProjectType.getProjectTypeId())){
                        mProjectType.setUpdateDate(new Date());
                        mProjectType.setUpdator(userId);
                        mProjectTypeService.updateMProjectType(mProjectType);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("修改成功");
                        return tidings;
                      }else{
                          tidings.setStatus(Const.SUCCESS);
                          tidings.setMsg("项目类型已经存在");
                          return tidings;
                      }
                    }else if(MP.size()==0||MP==null){
                        mProjectType.setUpdateDate(new Date());
                        mProjectType.setUpdator(userId);
                        mProjectTypeService.updateMProjectType(mProjectType);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("修改成功");
                        return tidings;
                    }else{
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("项目类型已经存在");
                        return tidings;
                    }
                }else{
                    MP=mProjectTypeService.checkName(map);
                    if(MP.size()>0){
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("项目类型已经存在");
                        return tidings;
                    }else{
                        String id = UuidUtil.getNumFormatUUID();
                        mProjectType.setProjectTypeId(Long.parseLong(id));
                        mProjectType.setStatus("1");//1启用0禁用
                        mProjectType.setCorpId(corpId);
                        mProjectType.setCreator(userId);
                        mProjectType.setCreateDate(new Date());
                        mProjectType.setIsDel("0");
                        mProjectTypeService.saveMProjectType(mProjectType);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("添加成功");
                        return tidings;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("操作失败");
        }
        return tidings;
    }
    @RequestMapping("merchant/mProjectType/deleteMProjectType")
    @ResponseBody
    public Object deleteMProjectType(){
        PageData pd = this.getPageData();
        Tidings<String > tidings = new Tidings<String>();
        String mProjectId=pd.getString("projectTypeId");
        try {
            Integer count=mProjectTypeService.getCountFromMProject(mProjectId);
            if(count!=null&&count>0){
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("该收费项目类型正在使用不能删除");
            }else{
                mProjectTypeService.deleteMProjectType(Long.parseLong(mProjectId));
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除失败");
        }
        return tidings;
    }
}
